package baidu.ui.core
{
    import baidu.ui.events.*;
    import flash.display.*;
    import flash.events.*;
    import flash.system.*;
    import flash.text.*;
    import flash.utils.*;

    public class BUI extends flash.display.Sprite
    {
		protected var _x:Number;
		
		protected var _y:Number;
		
		protected var _width:Number;
		
		protected var _height:Number;
		
		protected var _enabled:Boolean;
		
		protected var instanceStyles:Object;
		
		protected var callLaterMethods:flash.utils.Dictionary;
		
		protected var invalidHash:Object;
		
		public static var defaultStyles:Object ={"textFormat":new flash.text.TextFormat("宋体", 12, 0, false, false, false, "", "", "left", 0, 0, 0, 0), "disabledTextFormat":new flash.text.TextFormat("宋体", 12, 10066329, false, false, false, "", "", "left", 0, 0, 0, 0)};
		
		public static var inCallLaterPhase:Boolean=false;
		
        public function BUI()
        {
            super();
            this.instanceStyles = {};
            this.invalidHash = {};
            this.callLaterMethods = new flash.utils.Dictionary();
            this._enabled = true;
            this.initUI();
            this.invalidate(baidu.ui.core.Invalidation.ALL);
            return;
        }

        public function invalidate(arg1:String="all", arg2:Boolean=true):void
        {
            this.invalidHash[arg1] = true;
            if (arg2)
            {
                this.callLater(this.draw);
            }
            return;
        }

        public function setPosition(arg1:Number, arg2:Number, arg3:Boolean=true):void
        {
            if (isNaN(arg1) || isNaN(arg2))
            {
                return;
            }
            this._x = arg1;
            this._y = arg2;
            super.x = Math.round(arg1);
            super.y = Math.round(arg2);
            if (arg3)
            {
                dispatchEvent(new baidu.ui.events.BUIEvent(baidu.ui.events.BUIEvent.POSITION_CHANGE));
            }
            return;
        }

        public override function get x():Number
        {
            return this._x;
        }

        public override function set x(arg1:Number):void
        {
            this.setPosition(arg1, this._y);
            return;
        }

        public override function get y():Number
        {
            return this._y;
        }

        public override function set y(arg1:Number):void
        {
            this.setPosition(this._x, arg1);
            return;
        }

        public function setSize(arg1:Number, arg2:Number, arg3:Boolean=true):void
        {
            if (this._width == arg1 && this._height == arg2)
            {
                return;
            }
            this._width = arg1;
            this._height = arg2;
            this.invalidate(baidu.ui.core.Invalidation.SIZE);
            if (arg3)
            {
                dispatchEvent(new baidu.ui.events.BUIEvent(baidu.ui.events.BUIEvent.SIZE_CHANGE, false));
            }
            return;
        }

        public function get spriteWidth():Number
        {
            return super.width;
        }

        public override function get width():Number
        {
            return this._width;
        }

        public override function set width(arg1:Number):void
        {
            if (this._width == arg1)
            {
                return;
            }
            this.setSize(arg1, this._height);
            return;
        }

        public override function get height():Number
        {
            return this._height;
        }

        public function get spriteHeight():Number
        {
            return super.height;
        }

        public override function set height(arg1:Number):void
        {
            if (this._height == arg1)
            {
                return;
            }
            this.setSize(this._width, arg1);
            return;
        }

        public function get enabled():Boolean
        {
            return this._enabled;
        }

        public function set enabled(arg1:Boolean):void
        {
            if (arg1 == this._enabled)
            {
                return;
            }
            this._enabled = arg1;
            this.invalidate(baidu.ui.core.Invalidation.STATE);
            return;
        }

        public function drawNow():void
        {
            this.draw();
            return;
        }

        public function get classStyles():Object
        {
            return defaultStyles;
        }

        public function setStyle(arg1:String, arg2:Object):void
        {
            if (this.instanceStyles[arg1] === arg2 && !(arg2 as flash.text.TextFormat))
            {
                return;
            }
            this.instanceStyles[arg1] = arg2;
            this.invalidate(baidu.ui.core.Invalidation.STYLES);
            return;
        }

        public function setStyles(arg1:Object):void
        {
            var loc1:*;
            loc1 = null;
            var loc2:*;
            loc2 = 0;
            var loc3:*;
            loc3 = arg1;
            for (loc1 in loc3)
            {
                this.setStyle(loc1, arg1[loc1]);
            }
            return;
        }

        public function getStyle(arg1:String):Object
        {
            return this.instanceStyles[arg1];
        }

        public function clearStyle(arg1:String):void
        {
            this.setStyle(arg1, null);
            return;
        }

        protected function initUI():void
        {
            this.setPosition(0, 0);
            return;
        }

        protected function draw():void
        {
            this.validate();
            return;
        }

        protected function validateNow():void
        {
            this.invalidate(baidu.ui.core.Invalidation.ALL, false);
            this.draw();
            return;
        }

        protected function isInvalid(arg1:String, ... rest):Boolean
        {
            if (this.invalidHash[arg1] || this.invalidHash[baidu.ui.core.Invalidation.ALL])
            {
                return true;
            }
            while (rest.length > 0)
            {
                if (!this.invalidHash[rest.pop()])
                {
                    continue;
                }
                return true;
            }
            return false;
        }

        protected function validate():void
        {
            this.invalidHash = {};
            return;
        }

        protected function callLater(arg1:Function):void
        {
            var loc1:*;
            loc1 = null;
            if (inCallLaterPhase)
            {
                return;
            }
            this.callLaterMethods[arg1] = true;
            if (stage == null)
            {
                addEventListener(flash.events.Event.ADDED_TO_STAGE, this.callLaterDispatcher, false, 0, true);
            }
            else
            {
                loc1 = flash.system.Capabilities.os.toLowerCase().indexOf("linux") == -1 ? flash.events.Event.RENDER : flash.events.Event.ENTER_FRAME;
                stage.addEventListener(loc1, this.callLaterDispatcher, false, 0, true);
                if (loc1 == flash.events.Event.RENDER)
                {
                    stage.invalidate();
                }
            }
            return;
        }

        private function callLaterDispatcher(arg1:flash.events.Event):void
        {
            var loc1:*;
            loc1 = null;
            var loc3:*;
            loc3 = null;
            if (arg1.type == flash.events.Event.ADDED_TO_STAGE)
            {
                removeEventListener(flash.events.Event.ADDED_TO_STAGE, this.callLaterDispatcher);
                loc1 = flash.system.Capabilities.os.toLowerCase().indexOf("linux") == -1 ? flash.events.Event.RENDER : flash.events.Event.ENTER_FRAME;
                stage.addEventListener(loc1, this.callLaterDispatcher, false, 0, true);
                stage.invalidate();
                return;
            }
            loc1 = flash.system.Capabilities.os.toLowerCase().indexOf("linux") == -1 ? flash.events.Event.RENDER : flash.events.Event.ENTER_FRAME;
            arg1.target.removeEventListener(loc1, this.callLaterDispatcher);
            if (stage == null)
            {
                addEventListener(flash.events.Event.ADDED_TO_STAGE, this.callLaterDispatcher, false, 0, true);
                return;
            }
            inCallLaterPhase = true;
            var loc2:*;
            loc2 = this.callLaterMethods;
            var loc4:*;
            loc4 = 0;
            var loc5:*;
            loc5 = loc2;
            for (loc3 in loc5)
            {
                loc3();
                delete loc2[loc3];
            }
            inCallLaterPhase = false;
            return;
        }

        protected function getStyleValue(arg1:String):*
        {
            return this.instanceStyles[arg1] != null ? this.instanceStyles[arg1] : this.classStyles[arg1];
        }

        public static function mergeStyles(... rest):Object
        {
            var loc4:*;
            loc4 = null;
            var loc5:*;
            loc5 = null;
            var loc1:*;
            loc1 = {};
            var loc2:*;
            loc2 = rest.length;
            var loc3:*;
            loc3 = 0;
            while (loc3 < loc2)
            {
                loc4 = rest[loc3];
                var loc6:*;
                loc6 = 0;
                var loc7:*;
                loc7 = loc4;
                for (loc5 in loc7)
                {
                    loc1[loc5] = rest[loc3][loc5];
                }
                loc3 = (loc3 + 1);
            }
            return loc1;
        }

        public static function getSkinInstance(arg1:*):flash.display.DisplayObject
        {
            var classDef:Class;
            var skin:*;

            var loc1:*;
            skin = arg1;
            if (skin == null)
            {
                return null;
            }
            if (skin as Class)
            {
                return new skin() as flash.display.DisplayObject;
            }
            if (skin as flash.display.DisplayObject)
            {
                (skin as flash.display.DisplayObject).x = 0;
                (skin as flash.display.DisplayObject).y = 0;
                return skin as flash.display.DisplayObject;
            }
            classDef = null;
            try
            {
                classDef = flash.system.ApplicationDomain.currentDomain.getDefinition(skin) as Class;
            }
            catch (e:Error)
            {
                classDef = null;
            }
            if (classDef == null)
            {
                return null;
            }
            return new classDef() as flash.display.DisplayObject;
        }
    
    }
}